package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.yidu.ruifeng_logistics.entity.*;
import com.yidu.ruifeng_logistics.service.LogisticsClientService;
import com.yidu.ruifeng_logistics.service.LogisticsRoleService;
import com.yidu.ruifeng_logistics.service.LogisticsStaffService;
import com.yidu.ruifeng_logistics.service.LogisticsUserRoleService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import com.yidu.ruifeng_logistics.vo.LogisticsModelFllVo;
import com.yidu.ruifeng_logistics.vo.LogisticsUserRoleSelectVo;
import com.yidu.ruifeng_logistics.vo.LogisticsUserRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户角色表(LogisticsUserRole)表控制层
 *
 * @author 张皓超
 * @since 2021-03-11 11:15:58
 */
@Api(description = "瑞丰物流用户角色接口")
@RestController
@RequestMapping("logisticsUserRole")
public class LogisticsUserRoleController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsUserRoleService logisticsUserRoleService;

    /**
     * 员工服务对象
     */
    @Resource
    private LogisticsStaffService logisticsStaffService;
    /**
     * 客户服务对象
     */
    @Resource
    private LogisticsClientService logisticsClientService;
    /**
     * 角色服务对象
     */
    @Resource
    private LogisticsRoleService logisticsRoleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public LogisticsUserRole selectOne(Integer id) {
        return this.logisticsUserRoleService.queryById(id);
    }


    /**
     * 查询全部
     *
     * @return
     */
    @ApiOperation(value = "分页显示", notes = "分页返回所有信息")
    @GetMapping("findAll")
    public Map findAll(Integer page, Integer limit) {

         PageHelper.startPage(page, limit);
        List<LogisticsUserRoleVo> logisticsUserRoleVos = logisticsUserRoleService.queryStaffIdReturnLiseObj();
        PageInfo<LogisticsUserRoleVo> info = new PageInfo<>(logisticsUserRoleVos);
        Map<String, Object> map = new HashMap();
        map.put("msg", "查询成功");
        map.put("count", info.getTotal());
        map.put("data", logisticsUserRoleVos);
        map.put("code", 0);

        return map;
    }

    @ApiOperation(value = "数据回显", notes = "返回一条数据")
    @PostMapping("show")
    public BaseMessage show(Integer id) {
        LogisticsUserRole logisticsUserRole = logisticsUserRoleService.queryById(id);
        return BaseMessage.success(logisticsUserRole);
    }

    @ApiOperation(value = "增加和修改", notes = "返回成功或失败")
    @PostMapping("addAndUpdate")
    public BaseMessage addAndUpdate(Integer suserRoleId,
                                    Integer jue,
                                    String quiz) {


        String[] split = quiz.split(",1.865ps");
        System.out.println(suserRoleId+" =id            "+split[0]+"             " +split[1]  +"       "  +jue);
        Integer rows = null;
        if (split[0]!=null&&split[0]!=""&&split[1]!=null&&split[1]!="") {
            Integer id = Integer.valueOf(split[0]);
            int fig=-1;
            rows = logisticsClientService.selectIdAndName(id, split[1]);
            if (rows>0) {
                rows = logisticsStaffService.queryUserNameAndIdReturnCount(split[1], id);
                fig = 0;
            } else {
                fig = 1;
            }
            if (suserRoleId != null) {
                LogisticsUserRole logisticsUserRole = new LogisticsUserRole();
                //修改
                if (fig == 0) {
                    //是客户
                    logisticsUserRole.setClientId(id);
                    logisticsUserRole.setSuserRoleId(suserRoleId);
                    logisticsUserRole.setRoleId(jue);
                    logisticsUserRoleService.update(logisticsUserRole);
                }else if (fig==1){
                    //不是客户
                    logisticsUserRole.setStaffId(id);
                    logisticsUserRole.setSuserRoleId(suserRoleId);
                    logisticsUserRole.setRoleId(jue);
                    logisticsUserRoleService.update(logisticsUserRole);
                }else{
                    return BaseMessage.fail();
                }
            } else {
                LogisticsUserRole logisticsUserRole = new LogisticsUserRole();
                //增加
                if (fig == 0) {
                    //是客户
                    logisticsUserRole.setClientId(id);
                    logisticsUserRole.setSuserRoleId(suserRoleId);
                    logisticsUserRole.setRoleId(jue);
                    logisticsUserRoleService.insert(logisticsUserRole);
                } else if (fig==1){
                    //不是客户
                    logisticsUserRole.setStaffId(id);
                    logisticsUserRole.setSuserRoleId(suserRoleId);
                    logisticsUserRole.setRoleId(jue);
                    logisticsUserRoleService.insert(logisticsUserRole);
                }else{
                    return BaseMessage.fail();
                }
            }
            return BaseMessage.success();
        } else {
            return BaseMessage.fail();
        }
    }

    @ApiOperation(value = "返回选择框需要的数据", notes = "返回客户员工的数据")
    @PostMapping("selectShowStaffAndClient")
    public BaseMessage selectShowStaffAndClient() {
        Map<String, Object> map = new HashMap<>();
        List<LogisticsClient> logisticsClients = logisticsClientService.queryAll(new LogisticsClient());
        List<LogisticsStaff> list1 = logisticsStaffService.queryAll(new LogisticsStaff());
        map.put("data1", logisticsClients);
        map.put("data2", list1);
        return BaseMessage.success(map);
    }

    @ApiOperation(value = "返回选择框需要的数据", notes = "返回客户员工的角色")
    @PostMapping("selectShowJue")
    public BaseMessage selectShowJue() {
        List<LogisticsRole> logisticsRoles = logisticsRoleService.queryAll(new LogisticsRole());
        return BaseMessage.success(logisticsRoles);
    }

}
